﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Dapper;
using DapperExtensions;
using DapperWrapper;
using ZL.CRM.CommonEntity.AccOpr.Web;
using ZL.CRM.CommonEntity.Peizh;
using ZL.Web.Utility.Config;
using ZLWeb.WebApiEntity;
using ZLWeb.WebApiEntity.WechatCardModel;
using ZLWeb.WebApiEntityBase;
using ZLWeb.WebApiEntityBase.Cache;
using ZLWeb.WebApiRepository.InterFace;
using ZilLion.Service.Core.CommonRepository;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZLWeb.WebApiInfrastructure.Base;
using ZilLion.Service.Core.WechatService.Domain;

namespace ZLWeb.WebApiRepository
{
    public class ChargeRepository : CRMSqlServerRespository<WebApiEntityBase.Cache.zzinfo>, IChargeRepository
    {
        public ChargeRepository(long? Accid) : base(Accid)
        {
        }

        public ChargeRepository(IDbContext context) : base(context)
        {
        }

        public ChargeRepository(IDbContext context, long? Accid) : base(context)
        {
            this.Accid = Accid.GetValueOrDefault();
        }

        public Thrpaylog ChargeNewSave(Thrpaylog log, short payway)
        {
            List<Action<IDbConnection>> paActions = new List<Action<IDbConnection>>();
            if (log.xlog != null)
            {
                var ywInfoparam = ExecuteProcAddParam(log.xlog, "#wxlog", true);
                paActions.Add(ywInfoparam);
            }
            else
            {
                Wxlog sb = new Wxlog();
                var ac = ExecuteProcAddParam(sb, "#wxlog", false);
                paActions.Add(ac);
            }
            Thrpaylog tl = new Thrpaylog();
            log.xlog.tlglywyyr = DateTime.Now;
            log.xlog.tlsj = DateTime.Now;
            log.xlog.tlglywjmmc = "微信支付";
            log.xlog.tlczrmc = "微信使用者";
            var item = GloabConfigCacheLoader.CreateGloabConfigCacheLoader(new WechatServiceContext()).GetZlcompanylistCacheItem();
            var Zlcompany = item.Zlcompanylists.FirstOrDefault(tr => tr.couserab == ZLConfig.Couserab);
            if (Zlcompany != null)
            {
                log.xlog.tlfjz = Zlcompany.cousercode + Guid.NewGuid().ToString().Replace("-", "").Substring(0, 17);
                DynamicParameters p = CreateDynamicParameters(
                    new ParamModel(ParamLabel: "payway", Val: payway),
                    new ParamModel(ParamLabel: "accid", Val: Accid));
                ExecuteProc(paActions, "pr_Thrpaylog_newsave_new", p, (con) =>
                {
                    tl.xlog = con.GetList<Wxlog>().FirstOrDefault();
                });
                return tl;
            }
            return null;
        }

        public Result ChargeResultQuery(short payway, string tllsh, string tlfjzt)
        {
            DynamicParameters p = CreateDynamicParameters(
            new ParamModel(ParamLabel: "payway", Val: payway),
            new ParamModel(ParamLabel: "tllsh", Val: tllsh),
            new ParamModel(ParamLabel: "tlfjz", Val: tlfjzt));
            Result resitem = new Result();
            Result ritem = ExecuteProcWithTemptable(resitem, "pr_ThrPayResult_query_new", "#result", false,
                p).FirstOrDefault();
            return ritem;
        }

        public ZL.CRM.CommonEntity.AccOpr.Web.AccThrCzYwcore ChargeAccure(ZL.CRM.CommonEntity.AccOpr.Web.AccThrCzYwdetail log, string tllshfjz, short payway)
        {
            string tllsh = tllshfjz.Substring(0, 12);
            string tlfjzt = tllshfjz.Substring(12);
            DynamicParameters p = CreateDynamicParameters(
            new ParamModel(ParamLabel: "payway", Val: payway),
            new ParamModel(ParamLabel: "tllsh", Val: tllsh),
            new ParamModel(ParamLabel: "tlfjz", Val: tlfjzt));
            List<Action<IDbConnection>> paActions = new List<Action<IDbConnection>>();
            var ywInfoparam = ExecuteProcAddParam(log, "#ywdetail", true);
            paActions.Add(ywInfoparam);
            var Ywcoreparam = ExecuteProcAddParam(log.Ywcore, "#ywcore", true);
            paActions.Add(Ywcoreparam);
            ZL.CRM.CommonEntity.AccOpr.Web.AccThrCzYwcore item = new ZL.CRM.CommonEntity.AccOpr.Web.AccThrCzYwcore();
            ExecuteProc(paActions, "pr_AccThrCz_Submit_new", p, (con) =>
            {
                item = con.GetList<ZL.CRM.CommonEntity.AccOpr.Web.AccThrCzYwcore>().FirstOrDefault();
            });
            //var item= ExecuteProcWithTemptable(log.Ywcore, "pr_AccThrCz_Submit_new", "#ywcore", true,
            //  p).FirstOrDefault();
            return item;
        }


        public Ywxx GetPayLog(string tllsh, string tlfjzt)
        {
            DynamicParameters p = CreateDynamicParameters(
                   new ParamModel(ParamLabel: "accid", Val: Accid),
            new ParamModel(ParamLabel: "tllsh", Val: tllsh),
            new ParamModel(ParamLabel: "tlfjzt", Val: tlfjzt));
            Ywxx resitem = new Ywxx();
            Ywxx ritem = ExecuteProcWithTemptable(resitem, "pr_ThrPaylogYwxx_query", "#Ywxx", false,
                p).FirstOrDefault();
            return ritem;
        }
    }
}
